package com.os.opencv.java.chapter13;

import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.highgui.HighGui;
import org.opencv.imgproc.Imgproc;
import org.opencv.videoio.VideoCapture;

public class VideoCap {

    public static void main(String[] args) {
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

        try {

            //创建videocapture对象并打开视屏文件
            VideoCapture vc = new VideoCapture();
            vc.open("pics/2023-11-29_19-50-14.mp4");
            //vc.open(0);

            //视屏打开失败时的处理
            if(!vc.isOpened()){
                System.out.println("unable to load video!");
                return;
            }

            //循环读取视屏
            while(true){
                Mat frame = new Mat();
                vc.read(frame);
                if(frame.empty())
                    break;
                //将读取的帧图像转化成canny边缘图并在屏幕上显示
                Mat canny = new Mat();
                Imgproc.Canny(frame, canny, 60, 200);
                HighGui.imshow("canny", canny);
                int keyboard = HighGui.waitKey(100);
                if(keyboard == 27)  //按住esc键退出
                    break;
            }

            vc.release();  //释放资源
            HighGui.waitKey(0);

        }catch (Exception e){
            e.printStackTrace();
        }

    }
}
